package Z.dailyExercise.May;

import java.util.*;

public class _1738找出第K大的异或坐标值 {
    public static void main(String[] args) {
        _1738找出第K大的异或坐标值 test = new _1738找出第K大的异或坐标值();
        int[][] matrix = {{5,2},{1,6}};
        test.kthLargestValue(matrix,1);

    }

    /**
     * 寻找规律 构建一个结果矩阵 与一个 k大小的队列
     * @param matrix
     * @param k
     * @return
     */

    public int kthLargestValue(int[][] matrix, int k) {
        int m = matrix.length;
        int n = matrix[0].length;

        int[][] res = new int[m][n];
        res[0][0] = matrix[0][0];
        List<Integer> resls = new ArrayList<>();
        resls.add(res[0][0]);
        for (int i = 1; i < m; i++) {
            res[i][0] = res[i-1][0] ^ matrix[i][0];
            resls.add(res[i][0]);
        }
        for (int i = 1; i < n; i++) {
            res[0][i] = res[0][i-1] ^ matrix[0][i];
            resls.add(res[0][i]);
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                res[i][j] = res[i-1][j] ^ res[i][j-1] ^ res[i-1][j-1] ^ matrix[i][j];
                resls.add(res[i][j]);
            }

        }

        Collections.sort(resls, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        System.out.println(resls);

        return resls.get(k-1);





    }

}
